package com.netjava.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.netjava.pojo.User;
import com.netjava.service.IUserService;

public class UserAction extends BaseAction{
	public ActionForward showAllUsers(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		IUserService userService=(IUserService)getBean("userService");
		
		int page=1;
		if(null!=request.getParameter("page")){
			 page=Integer.parseInt(request.getParameter("page"));
		}
		Integer num=15;
		//设置末页
		Integer lastPage;
		
		
		//得到所有的注册用户
		List<User> regUsers = userService.getAllUser();
		
		
		List<User> users=userService.getNumUsers(page, num);
		
		
		 if((regUsers.size()-1 )% num != 0) { 
			  lastPage = (regUsers.size()-1 ) / num + 1; 
		     } else { 
		    	 lastPage =(regUsers.size()-1 )/ num; 
		     } 
		 
		 if(users.size()<=0){
			 request.setAttribute("msg", "没有用户记录！");
			 request.setAttribute("flag",0);
		 }
	  else{
		  request.setAttribute("flag",1);
	  }
		//设置当前页
		 request.setAttribute("currentPage",page);
		  request.setAttribute("lastPage",lastPage);
		
		request.setAttribute("users", users);
		ActionForward forward=mapping.getInputForward();
		forward=mapping.findForward("showAll");
		return forward;
		
	}
	
	//通过Ajax删除用户，在页面局部刷新
	public ActionForward deleteUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=UTF-8"); 
		String name = request.getParameter("name");
		IUserService userService=(IUserService)getBean("userService");
		
		//根据名字删除用户
		User user = userService.getByUserName(name);

		PrintWriter out = response.getWriter(); 
		
			userService.delete(user);
			
			out.println("<font color='red'>用户名"+name+"已经被删除!</font>");
		
		
		out.flush(); 
		out.close();
		return null; 
		
	}
	
	//得到单个用户的信息
	public ActionForward getUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		String name = request.getParameter("userName");
		IUserService userService=(IUserService)getBean("userService");
		//根据名字查找用户
		User user = userService.getByUserName(name);
	    
		if(user!=null){
			
			 //拍到的商品记录
			 
		        int listSize=user.getEndauctions().size();
		
			
	        
		     //给页面传送分页参数
			 int pageSize = 5; 
			 
			 // 总共有多少页 
		     int totalPage; 
		     
		     // 当前页数 
		     int curPage; 
		     
		   
		     
		     // 如果这个 records 里面没有东西，则返回的所有的跟页面有关的参数都是 0  
		     if(listSize == 0) { 
		         request.setAttribute("beginPoint", 0); 
		         request.setAttribute("endPoint", 0); 
		         request.setAttribute("list", 0); 
		         request.setAttribute("curPage", 0); 
		         request.setAttribute("totalPage", 0); 
		         request.setAttribute("message", "没有任何记录！"); 
		         
		     } 
		     
		     else{ // 计算总共有多少页 
		     if(listSize % pageSize != 0) { 
		         totalPage = listSize / pageSize + 1; 
		     } else { 
		         totalPage = listSize / pageSize; 
		     } 
		     
		     // 从 request 参数中取出将要显示的页数 
		     String curpage = (String) request.getParameter("page"); 
		     // 将从 request 中取出的数转换成整型，并保存在当前页数里面 
		     if(null == curpage) { 
		         curPage = 1; 
		     } else { 
		          
		         // 如果转换过程出现异常，则指定 curPage 为 1 
		         try { 
		             curPage = Integer.parseInt(curpage); 
		         } catch (Exception e) { 
		             curPage = 1; 
		         } 
		         
		         // 如果请求的参数比总页数大，则返回总页数，如果小于1，则返回1 
		         if(curPage > totalPage) { 
		             curPage = totalPage; 
		         } else if(curPage <= 0) { 
		             curPage = 1; 
		         } 
		     } 
		     
		     // 定义每一页需要显示的 records 段根据当前页数变化来变化 
		     int beginPoint = pageSize * curPage - pageSize; 
		     int endPoint = beginPoint + pageSize; 
		     // 如果结束的 records 位置比当前 records 的长度还大，则指定 endPage 为当前 records 长度 
		     if(endPoint > listSize) { 
		         endPoint = listSize; 
		     } 

		  
		     // 设置 records 开始位置 
		     request.setAttribute("beginPoint", beginPoint); 
		     // 设置 records 结束位置 
		     request.setAttribute("endPoint", endPoint - 1); 
		   
		     // 将要显示的页面数 
		     request.setAttribute("curPage", curPage); 
		     // 总共的页面数 
		     request.setAttribute("totalPage", totalPage); 
		     
		    
		}
		}
		else{
			
			request.setAttribute("msg","你要查询的用户不存在，请检查是否输入正确的用户名！");
		}
		
	       
		request.getSession().setAttribute("user", user);
		ActionForward forward=mapping.getInputForward();
		forward=mapping.findForward("showUser");
		return forward;
		
	}
	//修改用户权限
	public ActionForward modifyUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		String name = request.getParameter("userName");
		int type =Integer.parseInt(request.getParameter("type"));
		
		IUserService userService=(IUserService)getBean("userService");
		//根据修改用户权限
		User user = userService.getByUserName(name);
		if(null!=user){
			user.setType(type);
			userService.attachDirty(user);
			request.getSession().setAttribute("user", user);
			request.setAttribute("msg", "修改用户权限成功！");
		}
		
		ActionForward forward=mapping.getInputForward();
		forward=mapping.findForward("showUser");
		return forward;
	}
	
	
	// 修改用户资料
	public ActionForward modifyinfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		   IUserService userService=(IUserService)getBean("userService");
		   User user=(User)request.getSession().getAttribute("loginUser");
			//修改后的真实姓名 
			String name=request.getParameter("Name");
			//修改后的性别
			String sex=request.getParameter("Sex");
			//修改后的地址
			String address=request.getParameter("Address");
			//修改后的电话
	        int telephone=Integer.parseInt(request.getParameter("Tel"));
			//修改后的email
	        String email=request.getParameter("Email");
	        String authcode=(String) request.getSession().getAttribute("userInfo.authcode");
			String code=request.getParameter("ValCode");
			
			//验证码正确就可以更新数据
			if(authcode.equals(code)){

				user.setRealName(name);
				user.setSex(sex);
				user.setUserAddress(address);
				user.setUserPhone(telephone);
				user.setEmail(email);
				try{
					userService.attachDirty(user);
					request.setAttribute("msg", "修改成功！");
				}catch(Exception e){
					request.setAttribute("msg", "修改不成功！");
				}
				
				
			}
			else{
				request.setAttribute("msg", "验证码出错！请重新填写。");
			}
	        
		 request.getSession().setAttribute("user", user);
		 ActionForward forward=mapping.getInputForward();
		 forward=mapping.findForward("modifyinfo");
		 return forward;
		
	 }
	// 修改用户密码
	public ActionForward modifyPassword(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		    IUserService userService=(IUserService)getBean("userService");
		    User user=(User)request.getSession().getAttribute("loginUser");
			//修改前的密码 
			String oldpassword=request.getParameter("Old");
			//修改后的密码
			String newspassword1=request.getParameter("New0");
			String newspassword2=request.getParameter("New1");
				if(user.getPassword().equals(oldpassword)){
					if(newspassword1.equals(newspassword2)){
					user.setPassword(newspassword1);	
					userService.attachDirty(user);
					 request.setAttribute("Msg","修改成功");
					}else{
						String Msg="两次输入不匹配";
					request.setAttribute("Msg",Msg);}
			}else{
				String Msg="输入的原始密码错误！";
				request.setAttribute("Msg",Msg);
			}
		
		 request.getSession().setAttribute("user", user);
		 ActionForward forward=mapping.getInputForward();
		 forward=mapping.findForward("modifyPassword");
		 return forward;
		
	 }


}
